package com.skill.shareskill.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.skill.shareskill.R;
import com.skill.shareskill.adapter.ActivityItemAdapter;
import com.skill.shareskill.bean.ActivityItem;
import com.skill.shareskill.service.LoadDataThread;
import com.skill.shareskill.utils.HttpUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/2/1 0001.
 */

public class FindNearbyFragment extends Fragment {
    private ListView lv_find_nearby;
    private Gson gson=new Gson();
    private ActivityItemAdapter adapter;
    private List<ActivityItem> activityItemList;
    private Handler handler=new Handler(){
        public void handleMessage(Message message){
            String activityJson= (String) message.obj;
            Log.d("network", "sb==>" + activityJson);
            activityItemList=gson.fromJson(activityJson, new TypeToken<List<ActivityItem>>(){}.getType());
            if(adapter==null){
                adapter=new ActivityItemAdapter(getActivity(),activityItemList);
                lv_find_nearby.setAdapter(adapter);
            }else{
                adapter.notifyDataSetChanged();
            }
        }

    };

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv_find_nearby=getActivity().findViewById(R.id.lv_find_nearby);
        String activityUrl= HttpUtils.BASE_PATH+"activity/queryAllActivity";
        LoadDataThread lat=new LoadDataThread(handler,activityUrl);
        lat.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_find_nearby,container,false);
//        lv_find_nearby=getActivity().findViewById(R.id.lv_find_nearby);
//        String activityUrl= HttpUtils.BASE_PATH+"activity.json";
//        LoadDataThread lat=new LoadDataThread(handler,activityUrl);
//        lat.start();
        return v;    }
}
